package controller;

import implementor.InquiryStateImp;
import implementor.PriorityImp;
import implementor.SalesPersonImp;
import implementor.LeadsImp;
import implementor.FollowUpActionImp;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Timestamp;

import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalTextFieldUI;
import javax.swing.table.DefaultTableModel;

import daoInterfaces.InquiryStateDao;
import daoInterfaces.PriorityDao;
import daoInterfaces.SalesPersonDao;
import daoInterfaces.LeadsDao;
import daoInterfaces.FollowUpActionDao;
import dto.InquiryState;
import dto.Priority;
import dto.SalesPerson;
import dto.LeadDto;


import model.FollowUpAction;
import model.Lead;
import model.Model;

import utility.ViewUtilites;
import view.leeds.FollowupActions;

public class FollowUpActionsController implements ActionListener
{
	FollowupActions view;
	Lead model;
	
	
	
	
	public FollowUpActionsController() 
	{
		
	}

	
	

	public FollowUpActionsController(FollowupActions view, Lead model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListener(this);
	}



	

	public FollowupActions getView() {
		return view;
	}




	public void setView(FollowupActions view) {
		this.view = view;
	}




	public Lead getModel() {
		return model;
	}




	public void setModel(Lead model) {
		this.model = model;
	}

	
	public void setComboStatus()
	{
		InquiryStateDao en=new InquiryStateImp();
		InquiryState[] inquiry=en.retrieveAll();
		String[] items=new String[inquiry.length+1];
		items[0]="--Select Status--";
		for(int i=1;i<=inquiry.length;i++)
		{
			items[i]=inquiry[i-1].getStateId()+"";
		}
		ViewUtilites.set_comboBox(view.getComboStatus(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getComboStatus());
	}
	
	public void setComboPriority()
	{
		PriorityDao att=new PriorityImp();
		Priority[] a=att.retrieveAll();
		String[] items=new String[a.length+1];
		items[0]="--Select Priority Name--";
		for(int i=1;i<=a.length;i++)
		{
			items[i]=a[i-1].getPriorityName();
		}
		ViewUtilites.set_comboBox(view.getComboLeadPriority(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getComboLeadPriority());
	}
	
	public void setTable()
	{
		
		FollowUpAction[] myFollow=model.getFoll();
		
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<myFollow.length;i++)
		{
			dm.addRow(new Object[]{myFollow[i].getActionName(),myFollow[i].getAttendedId(),myFollow[i].getFollowUpDate(),myFollow[i].getComments()});
		}
	}
	
	public void setComboAttended()
	{
		SalesPersonDao atten=new SalesPersonImp();
		SalesPerson[] attended=atten.retrieveAll();
		String[] items=new String[attended.length+1];
		items[0]="--Select SalesPerson Name--";
		for(int i=1;i<=attended.length;i++)
		{
			items[i]=attended[i-1].getSalesPersonName();
		}
		ViewUtilites.set_comboBox(view.getComboAttended(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.getComboAttended());
	}
	
	public void setComboNewFollowUp()
	{
		FollowUpActionDao foll=new FollowUpActionImp();
		FollowUpAction[] f=foll.retrieveAll();
		String[] items=new String[f.length+1];
		items[0]="--Select Action Name--";
		for(int i=1;i<=f.length;i++)
		{
			items[i]=f[i-1].getActionName();
		}
		ViewUtilites.set_comboBox(view.gettNewFollowup(), items);
		ViewUtilites.setAutoSuggestionsComboBox(view.gettNewFollowup());
	}
	
	public void react()
	{
		String NewAction=view.gettNewFollowup().getSelectedItem().toString();
		String result=view.getTextAreaResult().getText();
		String att=view.getComboAttended().getSelectedItem().toString();
		java.sql.Date dates=ViewUtilites.convert(view.getLblClick().getText());
		
		SalesPersonDao atte=new SalesPersonImp();
		SalesPerson a=atte.retrieve("salesPersonName",att);
		int id=a.getSalesPersonId();
		
		LeadsDao imp=new LeadsImp();
		LeadDto l=imp.getLatest();
		int enqCode=l.getEnquiryCode();
		
		InquiryStateDao en=new InquiryStateImp();
		InquiryState impl=en.retrieve("stateName", model.getStateName());
		int stateId=impl.getStateId();
		
		Timestamp t=ViewUtilites.getTime();
		
		FollowUpAction follow=new FollowUpAction(0, model.getEnquiryCode(), stateId, id, result, dates, t, NewAction, 0);
		FollowUpActionImp followupact=new FollowUpActionImp();
		try
		{
			followupact.add(follow);
			JOptionPane.showMessageDialog(null, "Data successfully added");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully added,Please try again !");
		
		}	
	}
	
	public void updateStatus()
	{
		InquiryStateDao in=new InquiryStateImp();
		InquiryState imple=in.retrieve("stateName", model.getStateName());
		int stateId=imple.getStateId();
		
		int s=Integer.parseInt(view.getComboStatus().getSelectedItem().toString());
		LeadsDao le=new LeadsImp();
		
		LeadDto lead=le.retrieve("enquiryCode",stateId+"");
		lead.setStateId(s);
		try
		{
			le.update(lead);
			JOptionPane.showMessageDialog(null, "Data successfully updated");
		}
	
		catch(SQLIntegrityConstraintViolationException e)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully updated,Please try again !");
		
		}	
		
		
		
	}
	
	public void updatePriority()
	{
		
		
		String p=view.getComboLeadPriority().getSelectedItem().toString();
		LeadsDao e=new LeadsImp();
		LeadDto es=e.retrieve("enquiryCode", model.getEnquiryCode()+"");
		es.setPriority(p);
		try
		{
			e.update(es);
			JOptionPane.showMessageDialog(null, "Data successfully updated");
		}
	
		catch(SQLIntegrityConstraintViolationException ee)
		{
			JOptionPane.showMessageDialog(null, "Name already exist, please use another one");
		}
		
		catch(Exception ee)
		{
			JOptionPane.showMessageDialog(null, "Data not successfully updated,Please try again !");
		
		}	
	}


	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		String k=arg0.getActionCommand();
		if(k.contains("Save"))
		{
			updatePriority();
		}
		if(k.contains("Save"))
		{
			updateStatus();
		}
		if(k.contains("Add"))
		{
			react();
		}
		if(k.contains("Send Email"))
		{
			
		}
		if(k.contains("Send SMS"))
		{
			
		}
		if(k.contains("Cl"))
		{
			ViewUtilites.makeDateComponent(view.getLblClick());
		}
		
	}
	

}
